require 'rake/clean'
require 'find'
CLOBBER.include('*.blktrace.*', '*.sar-bin')
OPT="-march=athlon64 -O3"
PREFIX="/home/znmeb/block"
WHERE="http://brick.kernel.dk/snaps"
BLKTRACE="blktrace-git-latest.tar.gz"
FIO="fio-git-latest.tar.gz"
SWWHERE = "http://oss.oracle.com/~mason/seekwatcher"
SEEKWATCHER = "seekwatcher-0.11"

desc "Install Dependencies"
task :install_dependencies do
  sh "sudo emerge -uvDN sysstat seekwatcher mplayer libtheora fio btrace"
end

desc "Get latest versions of block layer tools from upstream"
task :get_latest_block_layer_tools do
  rm_rf "blktrace"
  sh "wget -N #{WHERE}/#{BLKTRACE}"
  sh "tar xf #{BLKTRACE}"
  cd "blktrace"
  cp "Makefile", "Makefile.bak"
  sh "sed \'s:/usr/local:#{PREFIX}:\' Makefile.bak > Makefile"
  sh "make; make docs; make install"
  cd ".."

  rm_rf "fio"
  sh "wget -N #{WHERE}/#{FIO}"
  sh "tar xf #{FIO}"
  cd "fio"
  cp "Makefile", "Makefile.bak"
  sh "sed \'s:/usr/local:#{PREFIX}:\' Makefile.bak > Makefile"
  sh "make; make install"
  cd ".."

  rm_rf SEEKWATCHER
  sh "wget -N #{SWWHERE}/#{SEEKWATCHER}.tar.bz2"
  sh "tar xf #{SEEKWATCHER}.tar.bz2"
  cp "#{SEEKWATCHER}/seekwatcher", "#{PREFIX}/bin"
end

# command to kill running "sar" data collector
SARBGON = "kill -TERM \`ps -elf|grep sadc|grep -v grep|awk \'{print $4}\'\`"
def run_benchmark(label, program, devices)

  # start "sar"
  system "sar -o #{label}.sar-bin 10 999999 > /dev/null 2>&1 &"

  # define the "seekwatcher" command
  command = 
    "sudo seekwatcher -t #{label} -o #{label}.pdf -p #{program} -d #{devices}"

  # run it
  system command

  # do "sar" report
  system SARBGON # kill "sar"
  system "sar -f #{label}.sar-bin -A > #{label}.sar-report"

  # make a movie
  system "seekwatcher -t #{label} -o #{label}.mpg -d #{devices} --movie"

  # do "blkparse" report
  system "blkparse -s -t #{label} > #{label}.blkparse"

  # fix permissions
  system "sudo chown -R znmeb:users ."
end

desc "Benchmark PHP compile"
task :benchmark_PHP_compile do
  run_benchmark "php", "\'emerge php > /dev/null 2>&1\'", "/dev/sda"
end

desc "Benchmark kernel build"
task :benchmark_kernel_build do
  run_benchmark "kernel", "\'genkernel all > /dev/null 2>&1\'", "/dev/sda"
end

desc "Benchmark find"
task :benchmark_find do
  run_benchmark "find", "\'find / > /dev/null 2>&1\'", "/dev/sda"
end

#
#desc "Analyze Alioth Shootout Data"
#file "shootout.eps" do
#
#  where=":pserver:anonymous@cvs.alioth.debian.org:/cvsroot/shootout"
#  sh "cvs -d #{where} checkout shootout/website/websites/gp4/data"
#  sh "R --vanilla --slave < pivot.R"
#  sh "R --vanilla --slave < shootout.R"
#end
#
#desc "Download Ruby 1.8.6-p111 source"
#file "ruby-1.8.6-p111.tar.bz2" do
#  ok = /ruby-1.8.6-p111.tar.bz2: OK/
#  sh "svn update" # get the checksum files
#  sh "wget ftp://ftp.ruby-lang.org/pub/ruby/1.8/ruby-1.8.6-p111.tar.bz2"
#  raise "MD5 Sum Failed" if `md5sum -c ruby-1.8.6-p111.md5sum` !~ ok
#  raise "SHA256 Sum Failed" if `sha256sum -c ruby-1.8.6-p111.sha256sum` !~ ok
#end
#
#desc "Build Ruby 1.9 source directory"
#file "ruby-1.9" do
#  sh "svn co http://svn.ruby-lang.org/repos/ruby/trunk ruby-1.9"
#  cd "ruby-1.9"
#  sh "autoconf"
#  cd ".."
#end
#
#desc "Build Ruby 1.8.6-p111 source directory"
#file "ruby-1.8.6-p111" => ["ruby-1.8.6-p111.tar.bz2"] do
#  sh "tar xjf ruby-1.8.6-p111.tar.bz2"
#
#  # as currently shipped, non-standard "CFLAGS" don't get propagated during
#  # shared library builds. There are two places we need to hack to get this
#  # to work: ruby-1.8.6-p111/lib/mkmf.rb and ruby-1.8.6-p111/Makefile.in
#  cp "mkmf.rb", "ruby-1.8.6-p111/lib/"
#  cp "Makefile.in", "ruby-1.8.6-p111/"
#
#  cd "ruby-1.8.6-p111"
#  sh "autoconf"
#  cd ".."
#end
#
#desc "Build unoptimized Ruby"
#file "unopt-build" => ["ruby-1.8.6-p111"] do
#  copy_source "unopt", "ruby-1.8.6-p111"
#  configure "unopt", "unopt", "-O0 -mtune=generic", "no"
#  make_ruby "unopt"
#  test_ruby "unopt"
#end
#
#desc "Build optimized Ruby"
#file "opt-build" => ["ruby-1.8.6-p111"] do
#  copy_source "opt", "ruby-1.8.6-p111"
#  configure "opt", "opt", "#{OPT}", "no"
#  make_ruby "opt"
#  test_ruby "opt"
#end
#
#desc "Build gprof Ruby"
#file "gprof-build" => ["ruby-1.8.6-p111"] do
#  copy_source "gprof", "ruby-1.8.6-p111"
#  configure "gprof", "gprof", "-g -pg", "no"
#  make_ruby "gprof"
#  test_ruby "gprof"
#end
#
#desc "Build gcov Ruby"
#file "gcov-build" => ["ruby-1.8.6-p111"] do
#  copy_source "gcov", "ruby-1.8.6-p111"
#  configure "gcov", "gcov", "-v -save-temps -g -fprofile-generate --coverage -frandom-seed=0", 
#    "no"
#  make_ruby "gcov"
#  test_ruby "gcov"
#end
#
#desc "Build reopt Ruby"
#file "reopt-build" => ["ruby-1.8.6-p111"] do
#  copy_source "reopt", "ruby-1.8.6-p111"
#  configure "reopt", "reopt", "-v -save-temps #{OPT} -fprofile-use -frandom-seed=0", 
#    "no"
#  make_ruby "reopt"
#  test_ruby "reopt"
#end
#
#desc "Install unoptimized Ruby"
#file "unopt-install" => ["unopt-build"] do
#  cd "unopt-build"
#  sh "make install"
#  cd ".."
#end
#
#desc "Install optimized Ruby"
#file "opt-install" => ["opt-build"] do
#  cd "opt-build"
#  sh "make install"
#  cd ".."
#end
#
#desc "Install gprof Ruby"
#file "gprof-install" => ["gprof-build"] do
#  cd "gprof-build"
#  sh "make install"
#  cd ".."
#end
#
#desc "Install gcov Ruby"
#file "gcov-install" => ["gcov-build"] do
#  cd "gcov-build"
#  sh "make install"
#  cd ".."
#end
#
##desc "Build re-optimized Ruby"
##file "reopt-build" => ["ruby-1.8.6-p111"] do
#  #configure "gcov", "-fprofile-use -frandom-seed=0 #{OPT}", 
#    #"ruby-1.8.6-p111", "no"
#  #make_ruby "gcov"
#  #test_ruby "gcov"
##end
#
##desc "Install re-optimized Ruby"
##task :reopt_install => [:reopt_build] do
#  #cd "gcov-build"
#  #sh "make install"
#  #cd ".."
##end
#
#desc "Build opt-1.9 Ruby"
#file "opt-1.9-build" => ["ruby-1.9"] do
#
#  # make sure source is up to date
#  cd "ruby-1.9"
#  sh "svn update"
#
#  # hacks for benchmarking
#  # 1. "bm_loop_generator" has a memory leak in some runs ... delete it
#  # 2. Add my own "Hilbert" benchmark
#  rm "benchmark/bm_loop_generator.rb" if 
#     File.exist?("benchmark/bm_loop_generator.rb")
#  cp "../bm_znmeb_hilbert.rb", "benchmark/"
#  sh "autoconf"
#  cd ".."
#
#  copy_source "opt-1.9", "ruby-1.9"
#  configure "opt-1.9", "opt-1.9", "#{OPT}", "yes"
#  make_ruby "opt-1.9"
#  #test_ruby "opt-1.9" # doesn't work yet
#end
#
#desc "Install opt-1.9 Ruby"
#file "opt-1.9-install" => ["opt-1.9-build"] do
#  cd "opt-1.9-build"
#  sh "make install"
#  cd ".."
#end
#
#desc "Run the unoptimized benchmark"
#task :unopt_benchmark => ["unopt-install", "opt-1.9-build"] do
#  benchmark "unopt"
#end
#
#desc "Run the optimized benchmark"
#task :opt_benchmark => ["opt-install", "opt-1.9-build"] do
#  benchmark "opt"
#end
#
#desc "Run the gprof benchmark"
#task :gprof_benchmark => ["gprof-build", "opt-1.9-build"] do
#  benchmark "gprof"
#end
#
#desc "Run the gcov benchmark"
#task :gcov_benchmark => ["gcov-build", "opt-1.9-build"] do
#  benchmark "gcov"
#end
#
##desc "Run the re-optimized benchmark"
##task :run_reopt_benchmark => ["reopt-build", "opt-1.9-build"] do
#  #run_benchmark "gcov"
##end
#
#desc "Touch the sources"
#task :touch_sources do
#  Find.find("gcov-build") do |file_name|
#    next if file_name =~ /\.o$/
#    next if file_name =~ /\.gcno$/
#    next if file_name =~ /\.gcda$/
#    sh "touch #{file_name}"
#  end
#end
#
#desc "Make profiles"
#task :make_profiles do
#  profiles = "#{Dir.getwd}/opt-1.9-build/benchmark"
#  source = "#{Dir.getwd}/gprof-build"
#  ruby = "#{Dir.getwd}/gprof-install/bin/ruby"
#  mkdir_p "benchmark_profiles"
#
#  # collect *.gmon.out from previous benchmarks
#  Find.find(profiles) do |file_name|
#    next unless file_name =~ /\.gmon\.out$/ # looking for profile outputs
#    mv "#{file_name}", "benchmark_profiles"
#  end
#  mv "petstore/gmon.out", "benchmark_profiles/petstore.gmon.out" if 
#     File.exist? "petstore/gmon.out"
#
#  # do the individual profiles
#  cd "benchmark_profiles"
#  Find.find(".") do |file_name|
#    next unless file_name =~ /\.gmon\.out$/ # looking for profile outputs
#    outfile = file_name.sub(/\.gmon\.out/, "") # output file name
#    sh "gprof -I #{source} -b #{ruby} #{file_name} > #{outfile}.gprof"
#    sh "gprof -I #{source} -A #{ruby} #{file_name} > #{outfile}.ann"
#    sh "gprof -I #{source} -r #{ruby} #{file_name} > #{outfile}.funcord"
#  end
#
#  # do the merged profile
#  sh "gprof -I #{source} -s #{ruby} *.gmon.out" # merge the profiles
#  sh "gprof -I #{source}    #{ruby} gmon.sum > total.gprof"
#  sh "gprof -I #{source} -A #{ruby} gmon.sum > total.ann"
#  sh "gprof -I #{source} -r #{ruby} gmon.sum > total.funcord"
#  sh "gprof -I #{source} -A -y #{ruby} gmon.sum" # individual source files
#  cd ".."
#end
#
#desc "Make gcovs"
#task :make_gcovs do
#  dest = "#{Dir.getwd}/benchmark_gcovs"
#  summ = "#{dest}/summary"
#  mkdir_p dest
#  rm summ if File.exist?(summ)
#  cd "gcov-build"
#  GCOV="gcov -a -b -l -f "
#  Find.find(".") do |file_name|
#    sh "#{GCOV} #{file_name} >> #{summ}" if file_name =~ /\.c$/
#  end
#  Find.find(".") do |file_name|
#    cp "#{file_name}", dest if file_name =~ /\.gcov$/
#  end
#  cd ".."
#end
#
#desc "Get Pet Store"
#file "petstore" do
#  sh "svn co http://tw-commons.rubyforge.org/svn/petstore/trunk petstore"
#  cd "petstore"
#  sh "vi config/database.yml"
#  sh "rake RAILS_ENV=production db:reset"
#  cd ".."
#end
#
#desc "Benchmark Optimized Pet Store"
#task :benchmark_opt_pet_store do
#  AB = "/usr/sbin/ab"
#  URL = "http://localhost:3000/shop/viewCategory.shtml?category=DOGS"
#  sh "#{AB} -c 1 -n 5000 #{URL} 2>&1 | tee opt.pbchlog"
#end
#
#desc "Benchmark gprof Pet Store"
#task :benchmark_gprof_pet_store do
#  AB = "/usr/sbin/ab"
#  URL = "http://localhost:3000/shop/viewCategory.shtml?category=DOGS"
#  sh "#{AB} -c 1 -n 5000 #{URL} 2>&1 | tee gprof.pbchlog"
#end
#
#desc "Benchmark gcov Pet Store"
#task :benchmark_gcov_pet_store do
#  AB = "/usr/sbin/ab"
#  URL = "http://localhost:3000/shop/viewCategory.shtml?category=DOGS"
#  sh "#{AB} -c 1 -n 5000 #{URL} 2>&1 | tee gcov.pbchlog"
#end
#
#desc "Start optimized Pet Store"
#task :start_opt_pet_store => ["petstore", "opt-install"] do
#  start_petstore "opt"
#end
#
#desc "Start gprof Pet Store"
#task :start_gprof_pet_store => ["petstore", "gprof-install"] do
#  start_petstore "gprof"
#end
#
#desc "Start gcov Pet Store"
#task :start_gcov_pet_store => ["petstore", "gcov-install"] do
#  start_petstore "gcov"
#end
#
#desc "Build CodeAnalyst"
#task :build_code_analyst do
#  rm_rf "CodeAnaylystLinux_2.6.22"
#  sh "tar xzf ~znmeb/CodeAnaylystLinux_2.6.22.tar.gz"
#  cd "CodeAnaylystLinux_2.6.22"
#  sh "./autogen.sh"
#  sh "libtoolize --copy --force"
#  sh "./configure"
#  sh "make"
#  sh "sudo make install"
#  cd ".."
#end
#
#desc "Install Dependencies"
#task :install_dependencies do
#  RAILS="dev-ruby/rails doc mysql postgres sqlite sqlite3"
#  sh "sudo chmod o+w /etc/portage/package.use"
#  sh "echo '#{RAILS}' >> /etc/portage/package.use"
#  sh "sudo chmod o-w /etc/portage/package.use"
#  sh "sudo emerge -uvDN rails" # this will bring in everything else
#  sh "sudo emerge -uvDN apache-tools"
#  mysql_count = (`ps -elf|grep mysql|grep -v grep|wc -l`).to_i
#  if (mysql_count < 1) then
#    sh "sudo emerge --config mysql" # configure MySQL
#    sh "sudo rc-update add mysql default" # have it start at boot time
#    sh "sudo /etc/init.d/mysql start" # start it now
#    sh "mysql -u root -p < ../create_petstore_database.sql"
#  end
#end
